Completed
Push — master ( 9ae94f...036dfa )
by Taavo-Taur
01:13
created

integration.test.js ➔ ... ➔ ???   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
c 2
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 9.4285
1
import test from 'ava'
2
3
import {addVueAndFeathers, vueAndFeathersCleanup} from './helpers/before/feathers-and-vue-hookup'
4
import {addBasicService} from './helpers/before/feathers-hookup'
5
6
test.beforeEach(addVueAndFeathers)
7
test.beforeEach(addBasicService)
8
9
test.afterEach(vueAndFeathersCleanup)
10
11
test.cb('Use single item syncer if requested', t => {
12
	const {Vue} = t.context
13
14
	t.context.instance = new Vue({
15
		sync: {
16
			testVar: {
17
				service: 'test',
18
				id: function () {
19
					return 1
20
				}
21
			}
22
		},
23
		created() {
24
			this.$on('syncer-loaded', path => {
25
				t.is(path, 'testVar')
26
				t.deepEqual(this.testVar, {id: 1, tested: true})
27
				t.end()
28
			})
29
			this.$on('syncer-error', (path, error) => {
30
				console.error(path, error)
31
				t.fail(error)
32
				t.end()
33
			})
34
		}
35
	})
36
})
37
38
test.cb('Cleanup', t => {
39
	const {client, Vue} = t.context
40
41
	const instance = t.context.instance = new Vue({
42
		sync: {
43
			test: 'test'
44
		},
45
		created() {
46
			this.$on('syncer-loaded', () => {
47
				Vue.util.nextTick(() => {
48
					instance.$destroy()
49
				})
50
			})
51
			this.$on('syncer-error', (path, error) => {
52
				t.fail(error)
53
				t.end()
54
			})
55
		},
56
		destroyed: function () {
57
			function checkEventListenersAreEmpty(event) {
58
				if (client.io.listeners['test ' + event]) {
59
					t.is(client.io.listeners['test ' + event].length, 0)
60
				} else {
61
					t.pass()
62
				}
63
			}
64
65
			checkEventListenersAreEmpty('created')
66
			checkEventListenersAreEmpty('updated')
67
			checkEventListenersAreEmpty('patched')
68
			checkEventListenersAreEmpty('removed')
69
70
			// syncer value is null after deletion
71
			t.deepEqual(this.test, null)
72
73
			t.end()
74
		}
75
	})
76
})
77
78
test.cb('Synced key can\'t be directly overwritten', t => {
79
	const {Vue} = t.context
80
81
	t.context.instance = new Vue({
82
		sync: {
83
			test: 'test'
84
		},
85
		created() {
86
			this.$on('syncer-loaded', () => {
87
				Vue.util.nextTick(() => {
88
					this.test = 'Failed'
89
90
					t.not(this.test, 'Failed')
91
92
					t.end()
93
				})
94
			})
95
			this.$on('syncer-error', (path, error) => {
96
				t.fail(error)
97
				t.end()
98
			})
99
		}
100
	})
101
})
102
103
test.cb('Syncer can be configured in mixins', t => {
104
	const {Vue} = t.context
105
106
	t.context.instance = new Vue({
107
		mixins: [
108
			{
109
				sync: {
110
					mixedIn: 'test',
111
					overwritten: {
112
						service: 'test',
113
						id() {
114
							return 2
115
						}
116
					}
117
				}
118
			}
119
		],
120
		sync: {
121
			overwritten: {
122
				service: 'test',
123
				id() {
124
					return 1
125
				}
126
			},
127
			independant: 'test'
128
		},
129
		created() {
130
			this.$on('syncer-loaded', () => {
131
				if (this.$loadingSyncers) {
132
					return // Wait for all
133
				}
134
135
				t.deepEqual(this.mixedIn, {1: {id: 1, tested: true}, 2: {id: 2, otherItem: true}})
136
				t.deepEqual(this.overwritten, {id: 1, tested: true})
137
				t.deepEqual(this.independant, {1: {id: 1, tested: true}, 2: {id: 2, otherItem: true}})
138
				t.end()
139
			})
140
			this.$on('syncer-error', (path, error) => {
141
				t.fail(error)
142
				t.end()
143
			})
144
		}
145
	})
146
})
147
148
test('Refresh syncers', t => {
149
	const {service, Vue} = t.context
150
151
	// Don't send out events, callService won't work here
152
	service.filter(() => false)
153
154
	let instance
155
156
	async function runTests() {
157
		// Patch all
158
		await Promise.all([
159
			service.patch(1, {updated: 1}),
160
			service.patch(2, {updated: 1})
161
		])
162
163
		// Ensure update didn't get forwarded
164
		t.deepEqual(instance.testCol, {1: {id: 1, tested: true}, 2: {id: 2, otherItem: true}})
165
		t.deepEqual(instance.testVar, {id: 1, tested: true})
166
167
		// Update one
168
		await instance.$refreshSyncers('testCol')
169
		t.deepEqual(instance.testCol, {1: {id: 1, tested: true, updated: 1}, 2: {id: 2, otherItem: true, updated: 1}})
170
		t.deepEqual(instance.testVar, {id: 1, tested: true})
171
172
		// Update array
173
		await Promise.all([
174
			service.patch(1, {updated: 2}),
175
			service.patch(2, {updated: 2})
176
		])
177
		await instance.$refreshSyncers(['testCol', 'testVar'])
178
		t.deepEqual(instance.testCol, {1: {id: 1, tested: true, updated: 2}, 2: {id: 2, otherItem: true, updated: 2}})
179
		t.deepEqual(instance.testVar, {id: 1, tested: true, updated: 2})
180
181
		// Update all
182
		await Promise.all([
183
			service.patch(1, {updated: 3}),
184
			service.patch(2, {updated: 3})
185
		])
186
		await instance.$refreshSyncers()
187
		t.deepEqual(instance.testCol, {1: {id: 1, tested: true, updated: 3}, 2: {id: 2, otherItem: true, updated: 3}})
188
		t.deepEqual(instance.testVar, {id: 1, tested: true, updated: 3})
189
	}
190
191
	return new Promise((resolve, reject) => {
192
		instance = t.context.instance = new Vue({
193
			sync: {
194
				testCol: {
195
					service: 'test'
196
				},
197
				testVar: {
198
					service: 'test',
199
					id: function () {
200
						return 1
201
					}
202
				}
203
			},
204
			created() {
205
				const loaded = () => {
206
					if (this.$loadingSyncers) {
207
						return // Wait for all
208
					}
209
210
					this.$off('syncer-loaded', loaded)
211
					resolve(runTests())
212
				}
213
214
				this.$on('syncer-loaded', loaded)
215
				this.$on('syncer-error', (path, error) => {
216
					console.error(path, error)
217
					reject(error)
218
				})
219
			}
220
		})
221
	})
222
})
223